﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOAModels;
using TarreOA.TarreOABLL;

public partial class Flow_Info_WorkToNext : System.Web.UI.Page {
    public Flow_NewFlowStep fnf;
    public int flow_id;
    public int step_sort;
    public int work_id;
    public User curLoginUser;

    protected void Page_Load(object sender, EventArgs e) {
        if(!Page.IsPostBack) {
            if(Request.QueryString.Get("FLOW_ID") != null && Request.QueryString.Get("ST_SO") != null && Request.QueryString.Get("WO_ID") != null) {
                flow_id = int.Parse(Request.QueryString.Get("FLOW_ID"));
                step_sort = int.Parse(Request.QueryString.Get("ST_SO"));
                work_id = int.Parse(Request.QueryString.Get("WO_ID"));
                SetFlow_NewFlowStep();
                ViewState["flow_id"] = flow_id;
                ViewState["step_sort"] = step_sort;
                ViewState["work_id"] = work_id;
            }
        }
    }

    private void SetFlow_NewFlowStep() {
        curLoginUser = (User) Session["user"];
        Flow_NewFlowStep_Service fnfs_service = new Flow_NewFlowStep_Service();
        fnf = fnfs_service.FindToNext(flow_id, step_sort);
    }
    protected void BtnToNext_Click(object sender, EventArgs e) {
        if(ViewState["flow_id"] != null && ViewState["step_sort"] != null && ViewState["work_id"] != null) {
            Session.Remove("NEXT_USERS");
            Session.Remove("NEXT_STEP");
            flow_id = (int) ViewState["flow_id"];
            step_sort = (int) ViewState["step_sort"];
            work_id = (int) ViewState["work_id"];
            Flow_WorkProcess_Service fwp_service = new Flow_WorkProcess_Service();
            if(Request.Form.Get("nextid") != null && Request.Form.GetValues("zhuban") != null) {
                int next_id = int.Parse(Request.Form.Get("nextid"));
                string[] users = Request.Form.GetValues("zhuban");
                string zhuban = users[0];
                if(fwp_service.SubmitWork(work_id, step_sort, next_id, users, zhuban) != 0) {
                    MessageAwoke.Instance().AddToWorkFlow(zhuban, 0);
                    Response.Redirect("WillTransactWork.aspx");
                }
            } else {
                fwp_service.SubmitWork(work_id, step_sort, -1, null, null);
                Response.Redirect("WillTransactWork.aspx");
            }
        }
        SetFlow_NewFlowStep();
    }
    protected void BtnBack_Click(object sender, EventArgs e) {
        if (Request.QueryString["jumpType"].ToString().Equals("1"))
        {
            Response.Redirect("WillTransactWork.aspx");
        }
        else
        {
            Response.Redirect("ManagerWorkShow.aspx");
        }
        
    }
}
